package com.quincysx.customaidl.demo2;

import android.os.Binder;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Parcel;
import android.os.RemoteException;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.quincysx.customaidl.bean.Book;

public interface IOnNewBookArrivedListener extends IInterface {

    void onNewBookArrive(Book book) throws RemoteException;

    public abstract static class Stub extends Binder implements IOnNewBookArrivedListener {
        private static final String DESCRIPTOR = "com.quincysx.customaidl" +
                ".demo2.IOnNewBookArrivedListener";

        private static final int TRANSACTION_onNewBookArrive = 1;

        public Stub() {
            this.attachInterface(this, DESCRIPTOR);
        }

        public static IOnNewBookArrivedListener asInterface(IBinder binder) {
            if (binder == null) {
                return null;
            } else {
                IInterface iin = binder.queryLocalInterface(DESCRIPTOR);
                if (iin != null && iin instanceof IOnNewBookArrivedListener) {
                    return (IOnNewBookArrivedListener) iin;
                }
                return new Proxy(binder);
            }
        }

        @Override
        public IBinder asBinder() {
            return this;
        }

        @Override
        protected boolean onTransact(int code, @NonNull Parcel data, @Nullable Parcel reply, int
                flags) throws RemoteException {
            switch (code) {
                case INTERFACE_TRANSACTION:
                    data.writeString(DESCRIPTOR);
                    return true;
                case TRANSACTION_onNewBookArrive:
                    data.enforceInterface(DESCRIPTOR);
                    Book book;
                    if (data.readInt() == 0) {
                        book = null;
                    } else {
                        book = Book.CREATOR.createFromParcel(data);
                    }
                    onNewBookArrive(book);
                    reply.writeNoException();
                    return true;
                default:
                    return super.onTransact(code, data, reply, flags);
            }
        }

        private static class Proxy implements IOnNewBookArrivedListener {
            private IBinder mRemote;

            public Proxy(IBinder remote) {
                mRemote = remote;
            }

            public String getInterfaceDescriptor() {
                return DESCRIPTOR;
            }

            @Override
            public void onNewBookArrive(Book book) throws RemoteException {
                Parcel data = Parcel.obtain();
                Parcel reply = Parcel.obtain();
                try {
                    data.writeInterfaceToken(DESCRIPTOR);
                    if (book == null) {
                        data.writeInt(0);
                    } else {
                        data.writeInt(1);
                        book.writeToParcel(data, 0);
                    }
                    mRemote.transact(TRANSACTION_onNewBookArrive, data, reply, 0);
                    reply.readException();
                } finally {
                    data.recycle();
                    reply.recycle();
                }
            }

            @Override
            public IBinder asBinder() {
                return mRemote;
            }
        }
    }

}
